﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Utility
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public class FileIOUtility
    {

        public static string WriteRecordImage(string fileName, string DeviceSn, byte[] data)
        {
            try
            {
                string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "RecordImage", DeviceSn);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var filePath = path + "\\" + fileName;
                if (!File.Exists(filePath))
                    File.WriteAllBytes(filePath, data);
                return $"RecordImage/{DeviceSn}/" + fileName;
            }
            catch
            {
                return "";
            }
        }
        public static string WriteFaceImage(ulong code, byte[] data)
        {
            if (data == null) return "";
            try
            {
                var fileName = code + ".jpg";
                string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "FaceImage");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.WriteAllBytes(path + "\\" + fileName, data);
                return $"FaceImage/" + fileName;
            }
            catch
            {
                return "";
            }
        }

        public static string ReadFaceImageBase64(string filePaht)
        {

            try
            {
                var data = ReadFaceImage(filePaht);
                if (data != null)
                    return Convert.ToBase64String(data);
            }
            catch
            {
            }
            return null;
        }

        public static byte[] ReadFaceImage(string filePaht)
        {
            try
            {
                string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
                return File.ReadAllBytes(path + "\\" + filePaht);
            }
            catch
            {
            }
            return null;
        }
    }
}
